<?php
//index SERVER
	//include config and classe
	require_once("include.php");
	session_start();

	//connect database
	$link=mysql_connect(DATABASE_ADDRESS, DATABASE_USER, DATABASE_PASS);
	mysql_select_db(DATABASE_NAME);
	
	if (isset($_GET['page']) and $_GET['page']=="rss"){
		include("rss.php");
		exit();
	}
	

	//instancier l'utilisateur
	if (isset($_SESSION['active']) and $_SESSION['active'] == 1){
		$user = &$_SESSION['user'];
	}else{
		$user = new User();
		$_SESSION['user'] = $user;
		$_SESSION['active'] = 1;
		$user->check_cookie();
	}

	//deconnecter l'utilisateur
	if (isset($_GET['disconnect']) and $_GET['disconnect']=="ok" ){
		User::disconnect_user();
		$user=new User();
	}
	//identifier l'utilisateur
	if (isset($_POST['hidden']) and $_POST['hidden']=="user_login"){
		if (! $user->check_password($_POST['user_login'],$_POST['user_passwd'])){
			$user->message->addError("Login/password error");
			//log
			Log::add_log("bad password for ".mysql_escape_string($_POST['user_login']));
		}
	}	


	//system
	include("system_request.php");
	include("system_ajax.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><?php echo $html_title ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<?php  if (USER::detect_ie()){?>
		<link rel="stylesheet" href="style-IE.css" type="text/css" />
	<?php }else{?>
		<link rel="stylesheet" href="style.css" type="text/css" />	
	<?php }?>	
	<link rel="alternate" type="application/rss+xml" title="All type" href="index.php?page=rss&size=30&type=alltype" />
	<?php 
	foreach($typefiles as $element ){
	?>	
		<link rel="alternate" type="application/rss+xml" title="<?php print $element; ?>" href="index.php?page=rss&size=30&type=<?php print $element; ?>" />	
	<?php
	}
	?>
	
	<link rel="icon" type="image/png" href="img/favicon.ico" />
	<script type="text/javascript" src="fonction.js"></script>
	<script src="js/prototype.js" type="text/javascript"></script>
 	<script src="js/scriptaculous.js?load=effects" type="text/javascript"></script>
	 
 	
</head>
<body>

	<?php
	//authentification de l'utilisateur
		if($user->access == 0){
	?>
	<div id="total">
	<?php
		//affichage des erreurs
		$errors = $user->message->getError();
		if (count($errors)>0){
			echo "<div  onclick=\"Effect.Shake(this);\" id=\"div_error\">";
			foreach($errors as $error){
				echo "<p>$error</p>";
			}
			echo "</div>";
			echo "<script>var ele= document.getElementById('div_error');Effect.Shake(ele);</script>";
		}
		//affichage des infos
		$infos = $user->message->getInfo();
		if (count($infos)>0){
			echo "<div id=\"div_info\">";
			foreach($infos as $info){
				echo "<p>$info</p>";
			}
			echo "</div>";
		}
	?>
	<form action="index.php" name="identification" method="post" >
	<table>
	<caption>Search Server</caption>
	<tr>
	<td>Login</td><td><input type = "text" name="user_login" /></td>
	</tr>
	<tr>
	<td>Password</td><td><input type="password" name="user_passwd" /></td>
	</tr>
	<tr>
	<td align="center" width="30px" colspan ="2"><input type="hidden" name="hidden" value="user_login" /><input type="submit" value="Connect" /></td>
	</tr>
	</table>
	</form>
	</div>
	<script language="javascript">
    	document.identification.user_login.focus();
	</script>
	
	<?php 		
		}
	//si user authentifier	
		else{		
	?>
	<div id="total2">
	<!-- Banniere -->
	<div id="div_banniere">
	
	</div>
	<!-- Fin de la Bannieres -->
	
	
	<!-- Info user -->
	<div id="info_user">
	
	</div>
	<!-- End info user -->
	

	<div id="contain">
	<?php
		//affichage des erreurs
		$errors = $user->message->getError();
		if (count($errors)>0){
			echo "<div  onclick=\"Effect.Shake(this);\" id=\"div_error\">";
			foreach($errors as $error){
				echo "<p>$error</p>";
			}
			echo "</div>";
			echo "<script>var ele= document.getElementById('div_error');Effect.Shake(ele);</script>";
		}
	?>
	<div id="span_contain">
	<?php
	//gestion des differentes pages
			if ($_GET['page']=="admin"){
				include('page_admin.php');
			}elseif (in_array($_GET['page'],$typefiles) or $_GET['page']=="search"){
				include('page_file.php');
			}elseif ($_GET['page']=="user"){
				include('page_user.php');
			}elseif ($_GET['page']=="favorite"){
				include('page_favorite.php');	
			}elseif ($_GET['page']=="message"){
				include('page_message.php');	
			}elseif($_GET['page']=="don"){
				include("page_don.php");	
			}else{
				include('page_acceuil.php');
			}
	?>
	</div>
	</div>
	
	<!-- MENU -->
	<div id="search_menu" style="position:<?php print $user->getMenuStatus(); ?>" ;>
	<?php 
	$sql_query = mysql_query("	SELECT * from 
								".DATABASE_TABLE_MESSAGE." ,
								(SELECT last_message_read as number FROM ".DATABASE_TABLE_USER." WHERE login = '$user->login') as req
								WHERE ".DATABASE_TABLE_MESSAGE.".id > req.number
							");
	$nb_message = mysql_num_rows($sql_query);
	?>
	<img onclick="ajax_change_status_menu('<?php print $user->getMenuStatus(); ?>')" id="status_menu" style="width:15%;position:absolute;left: 85%" alt="<?php print $user->getMenuStatus() ?>" src="./img/<?php print $user->getMenuStatus() ?>.png" />
	<div id="menu1">
		<a href="index.php">Home</a><br />
		<?php if($user->access == 2){ ?><a href="index.php?page=admin">Admin</a><br /> <?php } ?>
		<a href="index.php?page=user">User settings</a><br />
		<a href="index.php?page=favorite">Favorites</a><br />
		<a href="index.php?page=message">Messages <?php if($nb_message>0)print '<span class="red" >('.$nb_message.')</span>'; ?></a><br />
		<br />
		<?php 
		foreach($typefiles as $element ){
		?>
		<a href="index.php?page=<?php print $element;?>&server=choice"><?php print $element;?></a><br />
		<?php } ?>
	</div>
	
	<div id="menu2">
		User : <?php print $user->login; ?><br />
		<a href="index.php?disconnect=ok" >disconnect</a><br />
		Last connect:<br />
		<?php print date( "G\hi d/m/y",$user->last_login); ?><br />
	</div>
	
	<div id="menu3">
		<form action="index.php" name="search" method="get" >
		<span>Search :</span><br />
		<input type="text" name="file"  style="width: 86%;" /><br />
		<input type="hidden" name="page" value="search" />
		
		<select name="server" style="width: 90%;">
			<option value="allserver">All server</option>
		<?php 
		$sql_query=mysql_query("SELECT name FROM ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
			while ($query = mysql_fetch_array($sql_query) ){
		?>
			<option value="<?php print $query['name'] ?>"><?php print $query['name'] ?></option>
		<?php } ?>
		</select><br />
		<select name="type" style="width: 90%;">
			<option value="alltype">All Type</option>
		<?php 
			foreach ($typefiles as $element ){
		?>
			<option value="<?php print $element ?>"><?php print $element ?></option>
		<?php } ?>
		</select><br />
		<input type="submit" value="Ok" id="searchsubmit" style="width: 90%;" />
		</form>
	</div>

	</div>

	<!-- END MENU -->
	<div id ="div_footer">
	<p>Pour toutes les remarques contactez moi</p>
	<a href="mailto:<?php print $email_from; ?>?subject=Search">PHP and CSS By Bewiwi</a>
	</div>
	<script language="javascript">
    	document.search.file.focus();
	</script>
	<?php
		}
	?>
	

	
	
	</div>
</body>
</html>
	<?php mysql_close($link)?>
